package com.ruike.alisurface.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.BuildConfig;
import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.https.SettingHttp;
import com.ruike.alisurface.serialport.MsgEventBus;
import com.ruike.alisurface.ui.settings.Settings_Aty;
import com.ruike.alisurface.utils.QrcodeUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.DeviceUtils;
import com.voodoo.lib_utils.SharedUtils;
import com.voodoo.lib_utils.imageLoader.GlideImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description: 登录界面
 */
public class Login_Aty extends BaseAty {
    @BindView(R.id.login_versionName_tv)
    TextView loginVersionNameTv;
    @BindView(R.id.login_imgcode)
    ImageView mCodeImg;
    @BindView(R.id.login_sn_tv)
    TextView loginSnTv;
    @BindView(R.id.login_name_et)
    EditText loginNameEt;
    @BindView(R.id.login_pwd_et)
    EditText loginPwdEt;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_login;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
        L.i("登录页==" + this.getClass().getName());
        startCountdownTimer(60, null, null);

    }

    @Override
    protected void initData() {
        loginVersionNameTv.setText(new StringBuffer(getResources().getString(R.string.versionNameStr)).append(BuildConfig.VERSION_NAME));
        loginSnTv.setText(new StringBuffer(getResources().getString(R.string.deviceSnStr)).append(DeviceUtils.getDeviceSn()));

        String jsonSN = "http://medicine.admin.shouhuojiyun.com/rk_medicine_android/index.html?sn=" + DeviceUtils.getDeviceSn();
        Bitmap SnBitmap = QrcodeUtils.createQrCode(jsonSN, 300, 300, BitmapFactory.decodeResource(getResources(), 0));
        assert SnBitmap != null;
        GlideImageLoader.getInstance().loadBitmap(this, SnBitmap, mCodeImg);
    }


    @OnClick({R.id.login_goback_imgv, R.id.login_tv})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.login_goback_imgv:
                startCountdownTimer(0, null, ShopMain_Aty.class);
                break;

            case R.id.login_tv:
                String username = loginNameEt.getText().toString();
                String pwd = loginPwdEt.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    showErrorToast("账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    showErrorToast("密码不能为空");
                    return;
                }
                showProgressDialog("正在登录中...");
                SettingHttp.post_Login(username, pwd, new ApiListener() {
                    @Override
                    public void onRequestStart(String requestUrl, int requestId) {

                    }

                    @Override
                    public void onRequestComplete(String requestUrl, int requestId) {
                        removeProgressDialog();
                    }

                    @Override
                    public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
                        L.i("登录页=onSuccess=" + requestJson);

                        JSONObject data = null;
                        try {
                            data = requestJson.getJSONObject("data");
                            String token = data.getString("token");
                            SharedUtils.getInstance().putString(ShareKey.TOKEN, token);

                            startActivity(Settings_Aty.class, null);
                            finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
                        L.i("登录页=onError=" + errorMsg);

                        showErrorToast(errorMsg);
                    }
                });
                break;

        }
    }

    public void onEventBusResult(MsgEventBus event) {
        super.onEventBusResult(event);
        if (event.getType().equals(MsgEventBus.Jumpbacktothebackground)) {
            L.i("登录页==" + "跳转后台");
            startActivity(Settings_Aty.class, null);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

}
